package com.dtx.spring.reflect;


import com.dtx.spring.controller.UserController;
import com.dtx.spring.pojo.User;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.math.BigDecimal;

public class ReflectTest {

    @Test
    public void test() throws Exception {
        Object object = new Object();
        User user = new User();
        Class<User> c1 = User.class;
        Class<? extends User> c2 = user.getClass();
        Class<?> c3 = Class.forName("java.lang.String");
//        System.out.println(c1.equals(c2));
//        System.out.println(c1 == c2);
        Class<?> c4 = object.getClass();
        Constructor<?>[] constructors = c4.getDeclaredConstructors();
//        for (Constructor<?> constructor : constructors) {
//            System.out.println(constructor);
//        }
        Method[] methods = c4.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }

    @Test
    public void test1() throws Exception {
        Class<User> clazz = User.class;

        User user = clazz.getDeclaredConstructor(String.class).newInstance("name");
        User user1 = clazz.getDeclaredConstructor(Integer.class, String.class, BigDecimal.class)
                .newInstance(36, "name", new BigDecimal("3.333333"));

    }

    @Test
    public void test2() throws Exception {
        Class<UserController> clazz = UserController.class;
        Method method = clazz.getMethod("addUser", null);
        RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
        String[] value = requestMapping.value();
        for (String s : value) {
            System.out.println(s);
        }
    }
}
